package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.beancontext.BeanContext;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //---1--分页
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> categoryPage=categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(categoryPage.getTotal(),categoryPage.getResult());
    }

    //---2--新增
    @Override
    public void saveOne(CategoryDTO categoryDTO) {
        Category category=new Category();

        BeanUtils.copyProperties(categoryDTO,category);
        //------缺少属性
        category.setStatus(StatusConstant.ENABLE); //1表示启用 0表示禁用
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateUser(BaseContext.getCurrentId());


        categoryMapper.saveOne(category);
    }


    @Override
    public void updateDTO(CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Category category=Category.builder().id(id).status(status).build();
        categoryMapper.update(category);
    }

    //删除分类
    @Override
    public void deleteById(Long id) {
        //--id、下绑定菜品
        Integer count=dishMapper.selectByCategoryID(id);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //--id、下绑定套餐
        count=setmealMapper.selectByCategoryID(id);
        if (count>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteById(id);
    }


    //分类类型查分类
    @Override
    public List<Category> selectByType(Integer type) {
        List<Category> categoryList=categoryMapper.selectByType(type);
        return categoryList;
    }
}
